package com.cqan.account.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.cqan.account.entity.Role;
import com.cqan.account.entity.User;
import com.cqan.account.service.RoleService;
import com.cqan.common.ViewName;
import com.cqan.controller.BaseController;
import com.cqan.exception.AppException;
import com.cqan.utils.encode.EncodeUtils;

@Controller
@RequestMapping("/role")
public class RoleController extends BaseController<Role,Long, RoleService>{

	@RequestMapping(value="/add",method=RequestMethod.POST)
	public  ModelAndView add(HttpServletRequest req, HttpServletResponse resp) throws AppException {
		Role role = new Role();
		role.setEnabled(true);
		role.setName("系统管理员");
		entityService.add(role);
		return new ModelAndView(ViewName.MAIN_HOME);
	}
	
	
	@RequestMapping(value="/index",method=RequestMethod.GET)
	public  ModelAndView list(HttpServletRequest req, HttpServletResponse resp) throws AppException {
		EncodeUtils.md5("/main/index.htm");
		List<Role> roles = entityService.getAll();
		List<User> users = roles.get(0).getUsers();
		for (User user : users) {
			System.err.println(user.getUsername());
		}
		return new ModelAndView(ViewName.ROLE_INDEX);
	}
	
	@RequestMapping(value="/delete",method=RequestMethod.POST)
	public  ModelAndView delete(HttpServletRequest req, HttpServletResponse resp) throws AppException {
		return null;
	}
	
	@RequestMapping(value="/update",method=RequestMethod.POST)
	public  ModelAndView update(HttpServletRequest req, HttpServletResponse resp) throws AppException {
		return null;
	}

	@Override
	@Autowired
	public void setEntityService(RoleService entityService) {
		this.entityService = entityService;
	}
}
